package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Orders;
import com.itheima.common.OrderStatus;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.exception.CustomException;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 提交订单！
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submitOrder(@RequestBody Orders orders){

        int row = orderService.add(orders);
        if(row > 0){
            return R.success("提交订单成功！");
        }else{
            return R.error("提交订单失败！");
        }
    }
    @GetMapping("/page")
    public R findPage(PageParam pageParam){

        try {
            //交代service
            IPage<Orders> list = orderService.findPage(pageParam);
            //返回结果
            return R.success(list);
        } catch (Exception e) {
            throw new CustomException("网络异常···");
        }
    }
    //zgr
//    zzzzzzzzzz
    /**
     * 用户订单分页查询
     *  @Description
     *   @param
     *   @Return
    */
    @GetMapping("/userPage")
    public R userPage(PageParam pageParam){

        IPage<Orders> ordersIPage = orderService.userPage(pageParam);

        return R.success(ordersIPage);
    }

    /**
     * 修改状态为已派送
     * @param orderStatus
     * @return
     */
    @PutMapping
    public R updateStatus(@RequestBody OrderStatus orderStatus){

        int row = orderService.updateStatus(orderStatus);

        if(row > 0){
            return R.success("状态修改成功！");
        }else{
            return R.error("状态修改失败！");
        }

    }
}
